cap log close
clear all
set more off
set matsize 11000
set maxvar 120000

*Main data pathway globals, set directory to clean data folder
global tables "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\ResultsRev\"
global results "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\ResultsRev\"
global cleandata "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\CleanData\"



use using "S:\Project\DemoSos2\data2020\DAR\DAR.dta", clear
duplicates drop

bysort lopenr: gen x=_n
count //  1,686,564
count if x>1 // 2
drop if x>1
drop x

rename dodsaar yr_death


ge idc10_1  = substr(diagnose_underliggende_k,1,1) if type_diagnose_kodeverk_k== "10"
ge idc10_2  = substr(diagnose_underliggende_k,2,2) if type_diagnose_kodeverk_k== "10"
destring idc10_2, replace

ge inf =  idc10_1=="A" | idc10_1=="B"  						if type_diagnose_kodeverk_k== "10"
ge cancer =  idc10_1=="C" | idc10_1 =="D" 					if type_diagnose_kodeverk_k== "10"
ge cancer_c =  idc10_1=="C"									if type_diagnose_kodeverk_k== "10"
ge cancer_d =  idc10_1=="D"									if type_diagnose_kodeverk_k== "10"
ge mental =  idc10_1=="F" 									if type_diagnose_kodeverk_k== "10"
ge heart =  idc10_1=="I" 									if type_diagnose_kodeverk_k== "10"
ge o_heart =  idc10_1=="R" & (idc10_2>=0 &  idc10_2<=9)		if type_diagnose_kodeverk_k== "10"
ge resp =  idc10_1=="J" 									if type_diagnose_kodeverk_k== "10"
ge digest =  idc10_1=="K" 									if type_diagnose_kodeverk_k== "10"
ge ext =   idc10_1=="V" | idc10_1=="W"  | idc10_1=="X" | idc10_1=="Y" | ( idc10_1=="U" & idc10_2>=01 & idc10_2<=03) 						if type_diagnose_kodeverk_k== "10"

keep lopenr yr_death idc10_1 idc10_2 inf cancer_c cancer_d mental heart o_heart resp digest ext type_diagnose_kodeverk_k diagnose_underliggende_k
merge 1:1 lopenr using "S:\Project\DemoSos2\data2020\Befolkn\Demogr\faste_oppl", keepusing(foedselsaar)

ge age = yr_death-foedselsaar 
*tab diagnose_underliggende_k  if age >= 55 & type_diagnose_kodeverk_k == "10" & idc10_1 =="I" & yr_death >1995 & yr_death <= 2002
drop if yr_death < 2020

ge 		dage = 1 if age >= 25 & age <=34
replace dage = 2 if age >= 35 & age <=44
replace dage = 3 if age >= 45 & age <=54
replace dage = 4 if age >= 55 & age <=64
replace dage = 5 if age >= 65 & age <=74
replace dage = 6 if age >= 75 & age <=.

ge d = 1
bys dage: egen pop = sum(d)
keep if age >= 25 & age <= 100
keep if yr_death == 2020 
ge total = yr_death==2020

collapse (mean) pop (sum) cancer_c ext resp heart mental, by(dage)
for var cancer_c ext mental resp heart: replace X = X/pop*100000

label var cancer_c 		"Malign Cancer"
label var ext 			"External Conditions" 
label var mental 	"Mental and Behavioral Disorders"
label var resp 		"Respiratory Conditions"
label var heart		"Cardiovascular Conditions"

line cancer_c ext mental resp heart dage, lcolor(red blue green black navy) lwidth(thick thick thick thick thick) graphregion(color(white)) bgcolor(white) ///
	xlabel(1 "25-34" 2 "35-44" 3 "45-54" 4 "55-64" 5 "65-74" 6 "75+") xtitle(Age at death) ytitle("Mortality rate (by 100,000)")
graph export  "$results\amr.pdf", as(pdf)  replace

